package fr.efrei.tradesys.struts.actions.connection;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fr.efrei.tradesys.hibernate.beans.AppUser;
import fr.efrei.tradesys.hibernate.factory.DaoFactory;

public class LoginAction extends ActionSupport
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private static Log				LOGGER						= LogFactory
																									.getLog(LoginAction.class);
	private String						username;
	private String						password;

	public String execute() throws Exception
	{
		LOGGER.info("Execution of " + this.getClass().getName());
		Map<String, Object> session = ActionContext.getContext().getSession();
		AppUser u = DaoFactory.getInstance().getAppUserDao().getAppUser(username);
		if ((u != null) && (username.compareTo(u.getLogin()) == 0)
				&& (password.compareTo(u.getPassword()) == 0))
		{
			session.put("logged-in", "true");
			session.put("user", u);
			return SUCCESS;
		}
		else
		{
			addActionError("The login or password you wrote is not correct.");
			return ERROR;
		}
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

}
